// utils/debug-address.js
// 收货地址数据调试工具

/**
 * 调试收货地址数据
 */
export const debugAddressData = {
  /**
   * 打印地址数据详情
   * @param {Object} address - 地址数据
   * @param {string} title - 标题
   */
  logAddress(address, title = '地址数据') {
    console.log(`=== ${title} ===`)
    console.log('完整数据:', address)
    console.log('字段映射:')
    console.log('- receiver_name (收货人姓名):', address.receiver_name)
    console.log('- receiver_phone (收货人电话):', address.receiver_phone)
    console.log('- province (省份):', address.province)
    console.log('- city (城市):', address.city)
    console.log('- district (区县):', address.district)
    console.log('- street (详细地址):', address.street)
    console.log('- is_default (是否默认):', address.is_default)
    console.log('- user_id (用户ID):', address.user_id)
    console.log('- id (地址ID):', address.id)
    console.log('================')
  },

  /**
   * 验证地址数据是否完整
   * @param {Object} address - 地址数据
   * @returns {Object} 验证结果
   */
  validateAddress(address) {
    const result = {
      isValid: true,
      missingFields: [],
      suggestions: []
    }

    // 检查必需字段
    const requiredFields = [
      'receiver_name',
      'receiver_phone', 
      'province',
      'city',
      'district',
      'street'
    ]

    requiredFields.forEach(field => {
      if (!address[field] || address[field].toString().trim() === '') {
        result.isValid = false
        result.missingFields.push(field)
      }
    })

    // 检查字段类型
    if (address.is_default !== undefined && typeof address.is_default !== 'number') {
      result.suggestions.push('is_default 字段应该是数字类型 (0 或 1)')
    }

    if (address.user_id !== undefined && typeof address.user_id !== 'number') {
      result.suggestions.push('user_id 字段应该是数字类型')
    }

    return result
  },

  /**
   * 格式化地址显示
   * @param {Object} address - 地址数据
   * @returns {string} 格式化后的地址字符串
   */
  formatAddress(address) {
    const parts = []
    
    if (address.province) parts.push(address.province)
    if (address.city) parts.push(address.city)
    if (address.district) parts.push(address.district)
    if (address.street) parts.push(address.street)
    
    return parts.join('')
  },

  /**
   * 检查地址列表数据
   * @param {Array} addressList - 地址列表
   */
  checkAddressList(addressList) {
    console.log(`=== 地址列表检查 (共${addressList.length}条) ===`)
    
    if (!Array.isArray(addressList)) {
      console.error('❌ 地址列表不是数组类型')
      return
    }

    if (addressList.length === 0) {
      console.log('⚠️ 地址列表为空')
      return
    }

    addressList.forEach((address, index) => {
      console.log(`\n--- 地址 ${index + 1} ---`)
      this.logAddress(address, `地址 ${index + 1}`)
      
      const validation = this.validateAddress(address)
      if (!validation.isValid) {
        console.error('❌ 数据不完整，缺少字段:', validation.missingFields)
      } else {
        console.log('✅ 数据完整')
      }
      
      if (validation.suggestions.length > 0) {
        console.warn('⚠️ 建议:', validation.suggestions)
      }
    })
  }
}

export default debugAddressData 